<?php
class finance_ctl_bill extends desktop_controller{
    var $flag = 'sale';
	public function sale(){
        $is_export = kernel::single('desktop_user')->has_permission('finance_export');#增加销售收退款单导出权限
        $this->title = '销售收退款单';
        $base_filter = array('fee_type_id'=>'1');
        //add by ldward 201812-19 过滤当前的店铺和只看自营店铺
        if (!$_POST['shop_id']) {
            $filter['shop_id'] = $this->shopIds;
        } else {
            $filter['shop_id'] = $_POST['shop_id'];
        }
        $shopIds=app::get('ome')->model('shop')->getList("shop_id",array('shop_id' => $filter['shop_id'],'leemantype'=>'1' ));
        $shopIDs=array();
        foreach ($shopIds as $key => $value) 
        {
            array_push($shopIDs,$value['shop_id']);
        }
        $base_filter['channel_id'] = $shopIDs;//array('326ea317abbd35cf8498dd3d7eddab7d','9945246005908c5696c508ed70688265' );
        $this->flag = 'sale';
        $params = array(
            'title'=>$this->title,
            'actions'=>array(
                array(
                    'label' => '批量审核记账',
                    'submit' => 'index.php?app=finance&ctl=bill&act=do_charge&finder_id='.$_GET['finder_id'],
                ),
                array(
                    'label' => '添加账单',
                    'href' => 'index.php?app=finance&ctl=bill&act=add_bill&flt=sale&finder_id='.$_GET['finder_id'],
                    'target' => "dialog::{width:300,height:350,title:'添加账单'}",
                ),
                array(
                    'label' => '拉取账单',
                    'href' => 'index.php?app=finance&ctl=bill&act=trade_search&flt=sale&finder_id='.$_GET['finder_id'],
                    'target' => "dialog::{width:450,height:270,title:'拉取账单'}",
                ),
                array(
                    'label' => '天猫超市账单',
                    'href' => 'index.php?app=finance&ctl=bill&act=tmalltrade_search&flt=sale&finder_id='.$_GET['finder_id'],
                    'target' => "dialog::{width:550,height:270,title:'天猫超市账单'}",
                ),
            ),
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag'=>false,
            'use_buildin_recycle'=>false,
            'use_buildin_export'=>$is_export,
            'use_buildin_import'=>false,
            'use_buildin_filter'=>true,
            'use_view_tab'=>true,
            'finder_aliasname'=>'ar_sale',
            'finder_cols'=>'column_edit,bill_bn,channel_name,member,trade_time,order_bn,fee_item,money,status,unconfirm_money,confirm_money,charge_status,charge_time,monthly_status',
            'base_filter' => $base_filter,
        );
        $this->finder('finance_mdl_bill',$params);
    }

    public function index(){
        $this->title = '销售费用单';
        $this->flag = 'unsale';
        $base_filter = array('fee_type_id|noequal'=>'1');
        //add by ldward 201812-19 过滤当前的店铺和只看自营店铺
        if (!$_POST['shop_id']) {
            $filter['shop_id'] = $this->shopIds;
        } else {
            $filter['shop_id'] = $_POST['shop_id'];
        }
        $shopIds=app::get('ome')->model('shop')->getList("shop_id",array('shop_id' => $filter['shop_id'],'leemantype'=>'1' ));
        $shopIDs=array();
        foreach ($shopIds as $key => $value) 
        {
            array_push($shopIDs,$value['shop_id']);
        }
        $base_filter['channel_id'] = $shopIDs;//array('326ea317abbd35cf8498dd3d7eddab7d','9945246005908c5696c508ed70688265' );
        $this->base_filter = $base_filter;
        $params = array(
            'title'=>$this->title,
            'actions'=>array(
                array(
                    'label' => '批量审核记账',
                    'submit' => 'index.php?app=finance&ctl=bill&act=do_charge&finder_id='.$_GET['finder_id'],
                ),
                array(
                    'label' => '添加费用单',
                    'href' => 'index.php?app=finance&ctl=bill&act=add_bill&finder_id='.$_GET['finder_id'],
                    'target' => "dialog::{width:300,height:350,title:'添加费用单'}",
                ),
            ),
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag'=>false,
            'use_buildin_recycle'=>false,
            'use_buildin_export'=>true,
            'use_buildin_import'=>false,
            'use_buildin_filter'=>true,
            'use_view_tab'=>true,
            'finder_aliasname'=>'ar_unsale',
            'finder_cols'=>'bill_bn,channel_name,fee_obj,member,trade_time,order_bn,fee_type,fee_item,money,charge_status,charge_time,monthly_status,column_edit',
            'base_filter' => $base_filter,
        );
        $this->finder('finance_mdl_bill',$params);
    }

    function _views(){
        if($this->flag == 'sale'){
            $sub_menu = $this->_viewsale();
        }else{
            $sub_menu = $this->_viewunsale();
        }
        return $sub_menu;
    }

    function _viewsale()
    {

        //add by ldward 201812-19 过滤当前的店铺和只看自营店铺
        if (!$_POST['shop_id']) {
            $filter['shop_id'] = $this->shopIds;
        } else {
            $filter['shop_id'] = $_POST['shop_id'];
        }
        $shopIds=app::get('ome')->model('shop')->getList("shop_id",array('shop_id' => $filter['shop_id'],'leemantype'=>'1' ));
        $shopIDs=array();
        foreach ($shopIds as $key => $value) 
        {
            array_push($shopIDs,$value['shop_id']);
        }
        $arObj = $this->app->model('bill');
        $sub_menu = array(
            0 => array('label'=>app::get('base')->_('全部'),'filter'=>array('fee_type_id'=>1,'channel_id' => $shopIDs),'addon'=>$arObj->count(array('fee_type_id'=>1,'channel_id' => $shopIDs)),'optional'=>false),
            1 => array('label'=>app::get('base')->_('未记账'),'filter'=>array('fee_type_id'=>1,'charge_status'=>0,'channel_id' => $shopIDs),'addon'=>$arObj->count(array('fee_type_id'=>1,'charge_status'=>0,'channel_id' => $shopIDs)),'optional'=>false),
            2 => array('label'=>app::get('base')->_('待核销'),'filter'=>array('fee_type_id'=>1,'status|noequal'=>2,'charge_status'=>1,'channel_id' => $shopIDs),'addon'=>$arObj->count(array('fee_type_id'=>1,'status|noequal'=>2,'charge_status'=>1,'channel_id' => $shopIDs)),'optional'=>false),
            3 => array('label'=>app::get('base')->_('已核销'),'filter'=>array('fee_type_id'=>1,'status'=>2,'channel_id' => $shopIDs),'addon'=>$arObj->count(array('fee_type_id'=>1,'status'=>2,'channel_id' => $shopIDs)),'optional'=>false),
        );
        return $sub_menu;
    }

    function _viewunsale(){

        //add by ldward 201812-19 过滤当前的店铺和只看自营店铺
        if (!$_POST['shop_id']) {
            $filter['shop_id'] = $this->shopIds;
        } else {
            $filter['shop_id'] = $_POST['shop_id'];
        }
        $shopIds=app::get('ome')->model('shop')->getList("shop_id",array('shop_id' => $filter['shop_id'],'leemantype'=>'1' ));
        $shopIDs=array();
        foreach ($shopIds as $key => $value) 
        {
            array_push($shopIDs,$value['shop_id']);
        }
        $arObj = $this->app->model('bill');
        $sub_menu = array(
            0 => array('label'=>app::get('base')->_('全部'),'filter'=>array('fee_type_id|noequal'=>1,'channel_id' => $shopIDs),'addon'=>$arObj->count(array('fee_type_id|noequal'=>1,'channel_id' => $shopIDs)),'optional'=>false),
            1 => array('label'=>app::get('base')->_('未记账'),'filter'=>array('fee_type_id|noequal'=>1,'charge_status'=>0,'channel_id' => $shopIDs),'addon'=>$arObj->count(array('fee_type_id|noequal'=>1,'charge_status'=>0,'channel_id' => $shopIDs)),'optional'=>false),
            2 => array('label'=>app::get('base')->_('已记账'),'filter'=>array('fee_type_id|noequal'=>1,'charge_status'=>1,'channel_id' => $shopIDs),'addon'=>$arObj->count(array('fee_type_id|noequal'=>1,'charge_status'=>1,'channel_id' => $shopIDs)),'optional'=>false),
        );
        return $sub_menu;
    }

    //批量记账
    public function do_charge(){
        $this->begin('javascript:finderGroup["'.$_GET['finder_id'].'"].refresh();');
        $billObj = &app::get('finance')->model('bill');
        if($_POST['isSelectedAll'] == '_ALL_'){
            $where = $billObj->_filter($_POST);
            $sql = 'select bill_id from sdb_finance_bill where '.$where;
            $rs = kernel::database()->select($sql);
            $ids = array();
            foreach($rs as $v){
                $ids['bill_id'][] =$v['bill_id'];
            }
        }else{
            $ids = $_POST;
        }
        $res = kernel::single('finance_bill')->do_charge($ids);
        if($res['status'] == 'fail'){
            $this->end(false,$res['msg'],'javascript:finderGroup["'.$_GET['finder_id'].'"].refresh();');
        }
        $this->end(true,'操作成功！','javascript:finderGroup["'.$_GET['finder_id'].'"].refresh();');
    }

    //添加收退款单
    public function add_bill(){
        if($_GET['flt'] == 'sale'){
            $this->pagedata['fee_type_data'] = kernel::single('finance_bill')->get_fee_type_item_relation('sale');
        }else{ 
            $this->pagedata['fee_type_data'] = kernel::single('finance_bill')->get_fee_type_item_relation('unsale');
        }
        $this->pagedata['json'] = json_encode($this->pagedata['fee_type_data']);
        $this->pagedata['finder_id'] = $_GET['finder_id'];
        $this->display('bill/add.html');
    }

    //处理
    // ‘order_bn’=>’业务单据号bn’,
    //                     ‘member’=>’客户/会员或交易对方ID’,
    //                     ‘channel_id=>’渠道ID（店铺，仓库）’,
    //                     ‘channel_name’=>’渠道名称（店铺，仓库）’,
    //                     ‘trade_time’=>’单据的完成日期’,
    //                     ‘fee_obj’=>’费用对象’,
    //                     ‘money’=>’区分正负’,
    //                     ‘fee_item’=>’费用项名称’
    //                     'credential_number'=>'凭据号',
    public function do_add_bill(){
        $this->begin('javascript:finderGroup["'.$_GET['finder_id'].'"].refresh();');
        
        $orderdata = kernel::single('finance_func')->order_is_exists($_POST['order_bn']);
        if($orderdata == false){
            $this->end(false,'业务单据号不存在');
        }
        $shopinfo = kernel::single('finance_func')->getShopByShopID($orderdata['shop_id']);
        $sdf=array(
            'order_bn'=>$_POST['order_bn'],
            'member'=>$_POST['member'],
            'channel_id'=>$shopinfo['shop_id'],
            'channel_name'=>$shopinfo['shop_name'],
            'trade_time'=>$_POST['trade_time'],
            'fee_obj'=>$_POST['fee_obj'],
            'money'=>$_POST['money'],
            'fee_item'=>$_POST['fee_item'],
            'credential_number'=>$_POST['credential_number'],
            'unique_id'=>kernel::single('finance_func')->unique_id(array($_POST['credential_number'],$_POST['fee_obj'],$_POST['fee_item'])),
        );
        $init_flag = kernel::single('finance_monthly_report')->get_init_time();
        if($init_flag['flag'] === 'false') $this->end(false,'未设置初始化完成，不能添加');
        $res = kernel::single('finance_bill')->do_save($sdf);
        if($res['status'] == 'fail'){
            $this->end(false,$res['msg']);
        }
        $this->end(true,'添加成功');
    }

    //核销
    public function verificate(){
        $billObj = &app::get('finance')->model('bill');
        $bill_id = $_GET['bill_id'];
        $bill_data = kernel::single('finance_bill')->get_bill_by_bill_id($bill_id,'order_bn');
        $ar_data = kernel::single('finance_bill')->get_ar_by_bill_id($bill_id,'order_bn');
        $next_bill_id = kernel::single('finance_verification')->get_next_data($bill_id);
        $this->pagedata['bill_data'] = $bill_data;
        $this->pagedata['ar_data'] = $ar_data;
        $this->pagedata['bill_id'] = $bill_id;
        $this->pagedata['next_bill_id'] = $next_bill_id['bill_id'];
        $this->pagedata['finder_id'] = $_GET['finder_id'];
        if(isset($_GET['flag']) && $_GET['flag'] == 'replace'){
            $this->pagedata['replace'] = true;
        }else{
            $this->pagedata['replace'] = false;
        }
        $html = $this->fetch('bill/verificate.html');
        echo $html;
    }

    //分类展示
    public function sync_verificate(){
        $bill_id = $_POST['bill_id'];
        $flag = $_POST['flag'];
        $bill_data = kernel::single('finance_bill')->get_bill_by_bill_id($bill_id,$flag);
        $ar_data = kernel::single('finance_bill')->get_ar_by_bill_id($bill_id,$flag);
        $data['bill_data'] = $bill_data;
        $data['ar_data'] = $ar_data;
        echo json_encode($data);
    }

    //收支单导入 - 账单导入
    public function bill_import(){
        // 显示tab
        $settingTabs = array(
            array('name' => '支付宝账单导入', 'file_name' => 'bill/import/zhifubao.html', 'app' => 'finance','order'=>'100'),
            array('name' => '京东账单导入', 'file_name' => 'bill/import/jingdong.html', 'app' => 'finance','order'=>'200'),
            array('name' => '一号店账单导入', 'file_name' => 'bill/import/yihaodian.html', 'app' => 'finance','order'=>'300'),
            array('name' => '销售实收账单', 'file_name' => 'bill/import/normal.html', 'app' => 'finance','order'=>'400'),
            array('name' => '销售应收账单', 'file_name' => 'bill/import/ar.html', 'app' => 'finance','order'=>'500'),
            array('name' => '天猫超市账单导入', 'file_name' => 'bill/import/tmall.html', 'app' => 'finance','order'=>'500'),
        );
        // 定位到具体的TAB
        if (isset($_GET['tab']) && isset($settingTabs[intval($_GET['tab'])])) $settingTabs[intval($_GET['tab'])]['current'] = true;
        $this->pagedata['settingTabs'] = $settingTabs;
        $get = kernel::single('base_component_request')->get_get();

        $this->pagedata['ctler'] = 'finance_mdl_bill';
        $this->pagedata['add'] = 'finance';

        unset($get['app'],$get['ctl'],$get['act'],$get['add'],$get['ctler']);
        $this->pagedata['data'] = $get;

        $ioType = array();
        foreach( kernel::servicelist('omecsv_io') as $aIo ){
            $ioType[$aIo->io_type_name] = '.'.$aIo->io_type_name;
        }

        
        $this->pagedata['ioType'] = $ioType;
       
        $this->page('bill/import.html');
    }
    //确认核销
    public function do_verificate(){
        $this->begin('javascript:finderGroup["'.$_POST['finder_id'].'"].refresh();');
        if(empty($_POST['bill_id']) || empty($_POST['ar_id'])){
            $this->end(false,'应收单据或者实收单据不能为空');
        }
        $rs = kernel::single('finance_bill')->do_verificate($_POST['bill_id'],$_POST['ar_id'],$_POST['trade_time']);
        if($rs['status'] == 'fail'){
            $this->end(false,$rs['msg']);
        }
        $this->end(true,'操作成功');
    }

    //异步判断核销金额的大小
    public function sync_do_verificate(){
        if(empty($_POST['bill_id']) || empty($_POST['ar_id'])){
            $res = array('status'=>'fail','msg'=>'应收单据或者实收单据不能为空');
            echo json_encode($res);exit;
        }
        $res = kernel::single('finance_bill')->do_verificate($_POST['bill_id'],$_POST['ar_id'],$_POST['trade_time'],1);
        if($res['status'] == 'success'){
            switch ($res['msg_code']) {
                case '1':
                    $res['msg'] = '全额核销，是否确认？';
                    break;
                
                case '2':
                    $res['msg'] = '未核销应收合计大于实收，将按未核销金额由低到高的顺序核销，是否确认？';
                    break;
                case '3':
                    $res['msg'] = '未核销实收大于应收合计，实收账单将部分核销，是否确认？';
                    break;
            }
        }
        echo json_encode($res);
    }

    function findbill(){
        $filter = array('fee_type_id'=>'1','status|noequal'=>'2','charge_status'=>'1');
        $params = array(
            'title'=>'实收单据',
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag'=>false,
            'use_buildin_recycle'=>false,
            'use_buildin_export'=>false,
            'use_buildin_import'=>false,
            'use_view_tab' => false,
            'use_buildin_filter'=>true,
            'base_filter' => $filter,
        );
        $this->finder('finance_mdl_bill', $params);
    }

    public function getbill(){
        $billObj = &app::get('finance')->model('bill');
        $bill_id = $_POST['bill_id'];
        $data = $billObj->getList('*',array('bill_id'=>$bill_id));
        foreach($data as $k=>$v){
            $data[$k] = $v;
            $data[$k]['trade_time'] = date('Y-m-d',$v['trade_time']);
        }
        echo json_encode($data);
    }

    function findar(){
        $filter = array('status|noequal'=>'2','charge_status'=>'1');
        $params = array(
            'title'=>'应收单据',
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag'=>false,
            'use_buildin_recycle'=>false,
            'use_buildin_export'=>false,
            'use_buildin_import'=>false,
            'use_view_tab' => false,
            'use_buildin_filter'=>true,
            'base_filter' => $filter,
        );
        $this->finder('finance_mdl_ar', $params);
    }

    public function getar(){
        $arObj = &app::get('finance')->model('ar');
        $ar_id = $_POST['ar_id'];
        $data = $arObj->getList('*',array('ar_id'=>$ar_id));
        foreach($data as $k=>$v){
            $data[$k] = $v;
            $data[$k]['trade_time'] = date('Y-m-d',$v['trade_time']);
            $data[$k]['type'] = kernel::single('finance_ar')->get_name_by_type($v['type']);
        }
        echo json_encode($data);
    }

    public function sync_do_charge(){
        $res = array('status'=>'succ','msg'=>'');
        $bill_id = $_POST['bill_id'];
        $id = array('bill_id'=>array('0'=>$bill_id));
        $res = kernel::single('finance_bill')->do_charge($id);
        if($res['status'] == 'fail') {
            $res = array('status'=>'fail','msg'=>'记账失败');
            echo json_encode($res);
            exit;
        }
        $billObj = &app::get('finance')->model('bill');
        $cols = 'bill_id,bill_bn,member,order_bn,trade_time,credential_number,fee_obj,fee_item,money,unconfirm_money,confirm_money,charge_status';
        $data = $billObj->getList($cols,array('bill_id'=>$bill_id));
        foreach($data as $k=>$v){
            $data[$k]['trade_time'] = date("Y-m-d",$v['trade_time']);
        }
        $res['msg'] = $data[0];
        echo json_encode($res);
    }

    function sync_do_cancel(){
        $id = $_POST['id'];
        $finder_id = $_GET['finder_id'];
        $data = array('res'=>'succ','msg'=>'','finder_id'=>$finder_id);
        $billObj = &app::get('finance')->model('bill');
        $rs = $billObj->delete(array('bill_id'=>$id));
        if(!$rs){
            $data = array('res'=>'fail','msg'=>'作废不成功');
            echo json_decode($data);exit;
        }
        echo json_encode($data);
    }

    public  function  trade_search(){
        
        $this->pagedata['finder_id'] = $_GET['finder_id'];
        $shopMdl=app::get('ome')->model('shop');

        $shoplist=$shopMdl->getList("shop_id,name", array('leemantype' =>'1' ));
        $this->pagedata['shopList'] = $shoplist;
        $this->display('bill/trade_search.html');
    }
    public  function  tmalltrade_search()
    {


        $OBranch = app::get('ome')->model('branch');
        $branch  = $OBranch->getList('branch_id, name',array('type'=>array('main','damaged'),'b_type'=>1),0,-1);
        $allBranch  = $OBranch->getList('branch_id, name',array('b_type'=>1),0,-1);
        
        /*
         * 获取操作员管辖仓库
         */
        $is_super = kernel::single('desktop_user')->is_super();
        if (!$is_super){
           $branch_list = $OBranch->getBranchByUser();
           if(count($branch_list)>1){
               $from_branch_check = $branch_list[0]['branch_id'];
               $to_branch_check = $branch_list[1]['branch_id'];
           }
        }else{
           $branch_list = $branch;
           if(count($branch)>1){
               $from_branch_check = $branch[0]['branch_id'];
               $to_branch_check = $branch[1]['branch_id'];
           }
        }
        
        #过滤o2o门店虚拟物流公司
        $oDly_corp = app::get('ome')->model('dly_corp');
        $dly_corp = $oDly_corp->getlist('*',array('disabled'=>'false', 'd_type'=>'1'));
        $this->pagedata['dly_corp'] = $dly_corp;
        
        $this->pagedata['from_branch_check'] = $from_branch_check;
        $this->pagedata['to_branch_check'] = $to_branch_check;

        $this->pagedata['all_branch']   = $allBranch;
        $this->pagedata['branch_list']   = $branch_list;
        $this->pagedata['is_super']   = $is_super;

        $this->pagedata['operator'] = kernel::single('desktop_user')->get_name();
        $this->pagedata['branch'] = $branch ;
        $appropriation_type = app::get('ome')->getConf('taoguanallocate.appropriation_type');
        if (!$appropriation_type) $appropriation_type = 'directly';
        $this->pagedata['appropriation_type'] = $appropriation_type;        
        $this->pagedata['finder_id'] = $_GET['finder_id'];
        $shopMdl=app::get('ome')->model('shop');

        $shoplist=$shopMdl->getList("shop_id,name", array('leemantype' =>'1' ));
        $this->pagedata['shopList'] = $shoplist;
        $this->display('bill/tmalltrade_search.html');
    }

    public function do_trade_search(){
        $start_time = date("Y-m-d H:i:s",strtotime($_POST['start_time']));
        $end_time = date("Y-m-d H:i:s",strtotime($_POST['end_time']));
        $page_no = $_POST['page_no'];
        $alipay_order_no = $_POST['alipay_order_no'];
        $merchant_order_no = $_POST['merchant_order_no'];
        $shop_id=$_POST['shop_id'];
        $shopMdl=app::get('ome')->model('shop');
        $shop=$shopMdl->getList("shop_id,name,node_id", array('leemantype' =>'1','shop_id'=>$shop_id ));

        $tmp_num = 0;
         $data = kernel::single('erpapi_router_request')->set('shop', $shop_id)->finance_trade_search($start_time, $end_time, $page_no,100,$alipay_order_no,$merchant_order_no);
	if ($data['rsp'] == 'succ' ) {
            $record_list = isset($data['data']) && $data['data'] ? $data['data']['total_records'] : '';
            if ($record_list && is_array($record_list)) 
            {
                #添加账单
                kernel::single('finance_rpc_response_func_bill')->batch_trade_add($record_list, $shop[0]['node_id']);
            }
        }
        $tmp_num += $data['data']['total_pages'];
        if($tmp_num==$page_no && $data['rsp']=='succ'){
            echo 'finish@100';
        }elseif ($data['data']['total_pages']>$page_no && $data['rsp']=='succ'){
            $schedule = (100*$page_no)/$data['data']['total_results']*100;
            echo 'success@'.$schedule;
        }else{
            echo '获取失败';
        } 
    }
    //获取下载的淘宝文件名
    function gettaobaofile()
    {
        $postdata = array(
            'bill_date' => $_GET['bill_date'],
        );
        date_default_timezone_set("PRC");
        ini_set('soap.wsdl_cache_enabled', "0"); //关闭wsdl缓存
        ini_set('default_socket_timeout', 500);    // timeout
        $res=kernel::single('erpapi_rpc_qimen')->call('OmsGetAlipay',$postdata);
        $res=json_decode($res,1);
        if(isset($res['response']['data']) && !empty($res['response']['data']))
        {
            $data=json_decode($res['response']['data'],1);
            foreach ($data as $key => $value) 
            {
                $items[$key]['product_id']=$key;
                $items[$key]['fileurl']=$value["fileurl"];
                $items[$key]['filename']=$value["filename"];
            }
        }
        echo "window.autocompleter_json=".json_encode($items);
    }
    //导入数据
    function importtaobaobill()
    {
        $this->begin('index.php?app=finance&ctl=bill&act=sale');  
        $data=$_POST;
        $postdata = array();
        $i=0;
        foreach ($data['stockapp_ID'] as $key => $value) 
        {
            if($value['chk']=='on')
            {
                $i=$i+1;
                $postdata= array('fileurl' =>$value['fileurl'] );
            }
        }
        if($i!=1)
        {
            $this->end(false, '只允许选中一个文件！');
        }
        date_default_timezone_set("PRC");
        ini_set('soap.wsdl_cache_enabled', "0"); //关闭wsdl缓存
        ini_set('default_socket_timeout', 500);    // timeout
        $res=kernel::single('erpapi_rpc_qimen')->call('OmsImportAlipay',$postdata);
        $res=json_decode($res,1);
        if(isset($res['response']['data']) && !empty($res['response']['data']))
        {
            $data=json_decode($res['response']['data'],1);

            $billObj = kernel::single('finance_bill');
            #组织账单数据
            foreach ($data as $key => $value) 
            {
                //1、交易记录添加到销售实收

                $sdf1= array(
                    'order_bn'=>trim($value[1]),//'T200P207535325864157617',
                    'member'=>trim($value[10]),//'12312314123',
                    'channel_id'=>'326ea317abbd35cf8498dd3d7eddab7d',
                    'channel_name'=>'理文家居专营店（天猫）',
                    'trade_time'=>strtotime(trim($value[4])),//strtotime('2018-08-25 00:38:04'),
                    'fee_obj'=>'淘宝',
                    'money'=>trim($value[11]),//29.00,
                    'fee_item'=>'销售收款',
                    'credential_number'=>trim($value[0]),//'2018082522001121921001411827',
                    'unique_id'=>md5(trim($value[0])),
                    'charge_status'=>'0',
                    'memo'=>trim($value[3]),
                );
                $save_rs1=$billObj->do_save($sdf1);
                //2、费用添加到服务费
                if($value[22]!=0)
                {
                    $sdf= array(
                        'order_bn'=>trim($value[1]),
                        'member'=>trim($value[10]),
                        'channel_id'=>'326ea317abbd35cf8498dd3d7eddab7d',
                        'channel_name'=>'理文家居专营店（天猫）',
                        'trade_time'=>strtotime(trim($value[4])),//strtotime('2018-08-25 00:38:04'),
                        'fee_obj'=>'淘宝',
                        'money'=>-trim($value[22]),
                        'fee_item'=>'手续费',
                        'credential_number'=>trim($value[0]),
                        'unique_id'=>md5(trim($value[1])),
                        'charge_status'=>'0',
                        'memo'=>trim($value[3]),
                    );
                    $save_rs = $billObj->do_save($sdf);
                }
            }
        }
        $finder_id = $_GET['finder_id'];
        $this->end(true,"已导入成功!",'javascript:$("fm1").getParent(".dialog").retrieve("instance").close();finderGroup["'.$finder_id.'"].refresh();');
    }
}
